<template>
	<qh-page-view title="我的地址" boxShadow>
		<view class="list-box flex-column-100">
			<view class="list" v-for="(item, index) in address" :key="index" @tap.stop="checkaddress" :data-index="index">
				<view class="first_box">
					<view class="news">
						<span class="name">{{item.name}}</span>
						<span class="phone">{{item.phone}}</span>
					</view>
					<view @tap.stop="edit(item)" :data-id="item.id" class="edit_pic">
						<view>修改</view>
						<view class="iconfont iconfanhui" style="font-size: 26rpx;"></view>
					</view>
				</view>
				<view class="second_box">{{item.address}}</view>
				<view class="list-right-btn">
					<view style="display: flex;align-items: center;" :data-item="item" @tap.stop="setDefault">
						<!-- <image style="width: 100%;height: 100%" aspectFit src="/static/images/choose-1.png" mode="">
							</image> -->
						<uni-icons v-if="item.status==1" type="circle-filled" size="24" color="var(--primary-color)"></uni-icons>
						<!-- <image class="default_icon" v-else src="/static/images/choose-2.png" mode=""></image> -->
						<uni-icons type="circle" size="24" v-else color="#ccc"></uni-icons>
						<text class="adress-text-color">默认地址</text>
					</view>
					<view class="delete_text" v-if="type == 0" @tap.stop="del" :data-id="item.id">删除</view>
				</view>
			</view>
			<view v-if="empty" class="empty ">暂未设置收货地址</view>
		</view>
		<view class="position flex-cn-cn" :style="footerStyle">
			<view class="foot flex-cn-cn" @tap="add">
				<text>添加地址</text>
			</view>
		</view>
	</qh-page-view>
</template>

<script>
	var app = getApp().globalData;

	export default {
		data() {
			return {
				address: [],
				canclick: false,
				type: 0,
				empty: false
			};
		},
		computed: {
			footerStyle() {
				return `height:${120 + this.$store.bottomHeight}rpx;padding-bottom:${this.$store.bottomHeight}rpx`;
			},
		},
		onLoad(options) {
			if (options.type == 1) {
				this.canclick = true;
				this.type = 1;
			}
		},
		async onShow() {
			await this.$onLaunched;
			await this.getMyAddress();
		},
		methods: {
			getMyAddress() {
				let shoppingAddress = uni.getStorageSync('shoppingAddress')
				var that = this;
				getApp().globalData.sendRequest({
					url: '/Address/getAddress_v1',
					type: "post",
					success: function(res) {
						if (res.status == 1) {
							res.list.forEach(item => {
								item.origin_address = item.address;
								item.address = item.province + item.city + item.area + item.address +
									item.house_number
								if (shoppingAddress && shoppingAddress.id && item.id == shoppingAddress
									.id) {
									shoppingAddress = JSON.parse(JSON.stringify(item))
									uni.setStorageSync('shoppingAddress', shoppingAddress)
								}
							})
							that.setData({
								address: res.list,
								empty: false
							});

						} else {
							that.setData({
								address: [],
								empty: 1
							});
						}
					}
				});
			},
			add() {
				uni.navigateTo({
					url: '../addAddress/addAddress'
				});
			},

			setDefault: function(e) {
				var item = e.currentTarget.dataset.item;
				var that = this;
				getApp().globalData.sendRequest({
					url: '/Address/setDefault_v1',
					type: "post",
					data: {
						id: item.id
					},
					showLoading: false,
					success: function(res) {
						if (res.status == 1) {
							uni.showToast({
								title: res.msg
							})
							that.getMyAddress();
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1500,
								mask: true
							});
						}
					}
				});
			},
			del: function(e) {
				var id = e.currentTarget.dataset.id;
				var that = this;
				uni.showModal({
					title: '温馨提示',
					content: '确认删除该收货地址',
					showCancel: true,
					confirmColor: '#ff5c6d',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '删除中...',
								// #ifndef MP-ALIPAY
								mask: true,
								// #endif
							});
							getApp().globalData.sendRequest({
								url: '/Address/delAddress_v1',
								type: "post",
								data: {
									id: id
								},
								success: function(res) {
									uni.hideLoading();

									if (res.status == 1) {
										that.getMyAddress();
									} else {
										uni.showToast({
											title: res.msg,
											icon: 'none',
											duration: 1500,
											mask: true
										});
									}
								}
							});
						}
					}
				});
			},

			edit(item) {
				uni.setStorageSync('editAdress', item)
				uni.navigateTo({
					url: '../addAddress/addAddress?id=' + item.id + '&type=2'
				});

			},

			// 选择地址
			checkaddress: function(e) {
				var canclick = this.canclick;
				var address = this.address;

				if (!canclick) {
					return;
				} else {

					var index = e.currentTarget.dataset.index;
					var chooseAddress = address[index];
					// var info = '';

					// if (chooseAddress.province) {
					//   info = chooseAddress.province;
					// }

					// if (chooseAddress.city) {
					//   info += chooseAddress.city;
					// }

					// if (chooseAddress.province) {
					//   info += chooseAddress.area;
					// }

					// info += chooseAddress.address;
					// // info += chooseAddress.house_number;
					// chooseAddress.address = info;
					uni.setStorageSync('shoppingAddress', chooseAddress);

					this.$store.address_id = chooseAddress.id
					uni.navigateBack({
						//返回
						delta: 1
					});
				}
			},
		}
	};
</script>
<style>
	@import "./MyAddress.css";
</style>
